package com.olmarket.core.controller;

import com.olmarket.core.api.CommonPage;
import com.olmarket.core.api.CommonResult;
import com.olmarket.core.dto.GoodParam;
import com.olmarket.core.dto.RequestsParam;
import com.olmarket.core.dto.ResetPasswordParam;
import com.olmarket.core.entity.*;
import com.olmarket.core.service.StudentService;
import com.olmarket.core.service.UserLoginService;
import com.olmarket.core.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author h'zh
 * @date: 12.14
 */
@Api("负责处理管理员登录成功以后的各项功能")
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private StudentService studentService;

    // TODO: 管理员操作：(批量)删除用户、重置用户密码、管理商品&需求、管理商品类型
    /**
     * 学生管理
     */
    @ApiOperation(value = "批量删除学生", notes = "根据id(List）批量删除学生")
    @RequestMapping("/deleteStudents")
    public CommonResult deleteStudentBatch(@RequestBody List<Integer> StudentIds) {
        int i = userService.deleteStudentBatch(StudentIds);
        return i > 0 ? CommonResult.success("success") : CommonResult.failed("failed");
    }
    
    @ApiOperation(value = "重置学生密码", notes = "根据学生id重置密码为88888888")
    @RequestMapping("/resetStudentPwd/{id}")
    public CommonResult resetStudentPwd(@PathVariable int id) {
        int i = userService.updateStudentPassword(id, "88888888");
        return i >= 0 ? CommonResult.success(i) : CommonResult.failed("failed");
    }

    @ApiOperation(value = "更新学生信息", notes = "根据学生id更新学生信息")
    @RequestMapping(value = "modifyStudent/{id}", method = RequestMethod.POST)
    @Transactional
    public CommonResult modifyStudent(@PathVariable int id, @RequestBody UserStudent userStudent) {
        userStudent.setUserId(id);
        int i = userService.modifyStudent(userStudent);
        return i > 0 ? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "批量重置学生密码", notes = "根据学生id批量重置密码为88888888")
    @PostMapping("/resetStudentPwd")
    public CommonResult resetStudentPwd(@RequestBody ResetPasswordParam param) {
        int i = userService.resetStudentPwdBatch(param.getIds(), param.getPassword());
        return i > 0 ? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "根据关键词查询学生信息", notes = "通过关键字结合通配符来搜索数据库实现查询")
    @RequestMapping("/searchStudent")
    public CommonResult searchStudent(QueryInfo queryInfo) {
        String keyword = queryInfo.getKeyword();
        int type = queryInfo.getType();
        int pageNum = queryInfo.getPageNum();
        int pageSize = queryInfo.getPageSize();
        List<UserStudent> ret = userService.searchStudent(pageNum, pageSize, keyword, type);
        return CommonResult.success(CommonPage.restPage(ret));
    }

    /**
     * 学生管理结束
     */

    /**
     * 商品管理开始
     */

    @ApiOperation(value = "查询商品信息", notes = "根据关键词和类型查询")
    @RequestMapping("/searchGoods")
    public CommonResult searchGoods(QueryInfo queryInfo) {
        String keyword = queryInfo.getKeyword();
        int type = queryInfo.getType();
        int pageNum = queryInfo.getPageNum();
        int pageSize = queryInfo.getPageSize();
        List<GoodInfo> ret = userService.searchGoods(pageNum, pageSize, keyword, type);
        return CommonResult.success(CommonPage.restPage(ret));
    }

    @ApiOperation(value = "删除商品", notes = "根据id删除")
    @RequestMapping("/deleteGood/{id}")
    public CommonResult deleteGood(@PathVariable int id) {
        int i = userService.deleteGood(id);
        return i > 0 ? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "修改商品", notes = "根据商品")
    @RequestMapping("/modifyGood")
    public CommonResult modifyGood(@RequestBody GoodParam goodParam) {
        int i = userService.modifyGood(goodParam);
        return i > 0 ? CommonResult.success("success") : CommonResult.failed("failed");
    }

    //TODO:查看所有商品、修改、删除商品，方法与StudentController中类似，参数有变化

    /**
     * 商品管理部分结束
     */

    /**
     * 请求管理部分开始
     */

    @ApiOperation(value = "查询需求信息", notes = "根据关键词和类型查询")
    @RequestMapping("/searchRequests")
    public CommonResult searchRequests(QueryInfo queryInfo) {
        String keyword = queryInfo.getKeyword();
        int type = queryInfo.getType();
        int pageNum = queryInfo.getPageNum();
        int pageSize = queryInfo.getPageSize();
        List<RequestInfo> ret = userService.searchRequests(pageNum, pageSize, keyword, type);
        return CommonResult.success(CommonPage.restPage(ret));
    }

    @ApiOperation(value = "删除需求", notes = "根据id删除")
    @RequestMapping("/deleteRequest/{id}")
    public CommonResult deleteRequest(@PathVariable int id) {
        int i = userService.deleteRequest(id);
        return i > 0 ? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "修改需求", notes = "根据id修改")
    @RequestMapping("/modifyRequest")
    public CommonResult modifyRequest(@RequestBody RequestsParam requestsParam) {
        int i = userService.modifyRequest(requestsParam);
        return i > 0 ? CommonResult.success("success") : CommonResult.failed("failed");
    }


    //TODO:查看所有需求、修改、删除需求，方法与StudentController中类似，参数有变化

    /**
     * 请求管理部分结束
     */

    /**
     * 商品类型管理部分开始
     */
    @ApiOperation(value = "删除类型", notes = "根据类型Id")
    @RequestMapping("deleteType/{id}")
    public CommonResult deleteType(@PathVariable int id) {
        int i = userService.deleteType(id);
        return i > 0 ? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "创建类型", notes = "创建新的类型 ")
    @RequestMapping("/addType")
    public CommonResult addType(@RequestBody Type newType) {
        try {
            userService.addType(newType);
        } catch (Exception e) {
            return CommonResult.failed("failed");
        }
        return CommonResult.success("success");
    }

    @ApiOperation(value = "修改类型", notes = "修改指定的类型 ")
    @RequestMapping("/updateType")
    public CommonResult updateType(@RequestBody Type Type) {
        try {
            userService.updateType(Type);
        } catch (Exception e) {
            return CommonResult.failed("failed");
        }
        return CommonResult.success("success");
    }

    @ApiOperation(value = "查找类型", notes = "查找所有类型")
    @RequestMapping("/getAllTypes")
    public CommonResult getAllTypes() {
        List<Type> res;
        try {
            res = userService.getAllTypes();
        } catch (Exception e) {
            return CommonResult.failed("failed");
        }
        return CommonResult.success(res);
    }

}
